//
//  ModifyMobileVC.m
//  sensen_ios
//
//  Created by mc on 16/2/24.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ModifyMobileVC.h"

#import "LoginVC.h"

#import "UserOperationView.h"

@interface ModifyMobileVC ()

@property (nonatomic, strong) UserOperationView *userView;
@property (nonatomic, strong) UILabel *mobileLable;


@end

@implementation ModifyMobileVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //绑定手机
    self.title = @"绑定手机";
    
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
    
    // 导航栏左边返回按钮
    [self createLeftBarButtonItemWithImagename:nil];
    
    [self _createViews];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 导航栏样式
    [self isNaviTranslucent:NO statesLightContent:YES];
    // 把当前邮箱显示在输入框
    //UserModel *model = [CommonCache getUserInfo];
    
}

#pragma mark - 创建视图
- (void)_createViews {
    
    [self _createUserView];
    [self _createButtons];
    [self _createEmailView];
}
//创建邮箱按钮，只有修改邮箱状态下才有
- (void)_createEmailView {
    
    self.mobileLable = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH, lineHeight)];
    [self.scrollView addSubview:_mobileLable];
    
    UserModel *user = [CommonCache getUserInfo];
    if (user.mobile) {
        NSMutableString *name = [NSMutableString stringWithString:user.mobile];
        [name replaceCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        self.mobileLable.text = name;
        self.mobileLable.textColor = [UIColor darkGrayColor];

    }
}



// 创建用户操作视图（登陆，注册等）
- (void)_createUserView {
    
    NSArray * tempArray ;
    //UserModel *user = [CommonCache getUserInfo];
    
    tempArray = @[@"请输入当前密码", @"请输入手机号", @"请输入获取的六位验证码"];

    _userView = [[UserOperationView alloc] initWithFrame:CGRectMake(0, lineHeight, SCREEN_WIDTH, lineHeight * 4)
                                                  titles:tempArray
                                                    type:ZKUserViewChangeMobile];
    [self.scrollView addSubview:_userView];
}
- (void)_createButtons {
    CGFloat x = 20;
    CGFloat y = _userView.bottom + 20;
    CGFloat width = SCREEN_WIDTH - 2 * x;
    CGFloat height = 40;
    
    // 修改按钮
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    registerButton.frame = CGRectMake(x, y, width, height);
    [registerButton setTitle:@"确认修改" forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerButton addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.scrollView addSubview:registerButton];
    registerButton.tag = 99;
    registerButton.backgroundColor = SS_NAVI_BLUE_COLOR;
    registerButton.enabled = YES;;
    
    registerButton.layer.borderColor = SS_NAVI_BLUE_COLOR.CGColor;
    registerButton.layer.borderWidth = 1;
    registerButton.layer.masksToBounds = YES;
    registerButton.layer.cornerRadius = 3.0;

    
}

#pragma mark - 点击事件
- (void)registerAction:(UIButton *)button {
       

    [_userView getChangeMobileInfo:^(NSDictionary *infoDic) {
        
        UserModel *user = [CommonCache getUserInfo];
        user.username = infoDic[@"mobile"];
        user.password = infoDic[@"password"];
        // 获取userModel的字典形式
        NSLog(@"%@", infoDic[@"mobile"]);
        NSLog(@"%@", infoDic[@"password"]);
        NSLog(@"%@", infoDic[@"code"]);

        NSMutableDictionary *userDic = [user getDictionary];
        
        
            
        [userDic setObject:infoDic[@"code"] forKey:@"code"];
        [userDic setObject:infoDic[@"mobile"] forKey:@"mobile"];
        [userDic setObject:infoDic[@"password"] forKey:@"password"];
        [userDic setObject:user.uid forKey:@"uid"];
            
        [SVProgressHUD showWithStatus:@"正在修改中..."];
        [BoyeHttpClient post:@"BY_User_changePhone" apiVerson:100 params:userDic success:^(AFHTTPRequestOperation *operation, id result) {
        NSLog(@"zhanbin%@", result);
        [SVProgressHUD showSuccessWithStatus:@"手机号修改成功" withDuration:1];
                if ([result isEqualToString:@"绑定成功!"]) {
                    
                    // 保存到本地
                    user.mobile = infoDic[@"mobile"];
                    [CommonCache setUserInfo:user];
                    if (![user.reg_from isEqualToString:@"2"] ) {
                        
                       [self _userLogout];
                        
                    }else{

                        [self.navigationController popViewControllerAnimated:YES];
                    }
                }
                
                
                //[self.navigationController popViewControllerAnimated:YES];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@",error.domain] withDuration:1];
                NSLog(@"-------------%@", error);
            }];
            
            
        
        
            
            
            
        }];
}


// 用户退出账户
- (void)_userLogout
{
    // 清理本地账户信息
    [CommonCache logout];
    
    [SVProgressHUD showOnlyStatus:@"请重新登录！" withDuration:1.5];
    
    //[self.navigationController popViewControllerAnimated:YES];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//#pragma mark - 其他
//// 输入框内容改变通知事件
//- (void)allTextFieldEndEditing {
//    UIButton *reigsterButton = [self.view viewWithTag:99];
//    if ([_userView isAllTextFieldEdited]) {
//        reigsterButton.backgroundColor = SS_NAVI_BLUE_COLOR;
//        reigsterButton.enabled = YES;
//    } else {
//        reigsterButton.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
//        reigsterButton.enabled = NO;
//    }
//}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
